package com.ruoyi.common.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * JSON 文件转 Map 工具类
 */
public class JsonFileToMapUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 通用方法，自动识别JSON是对象还是数组
     */
    public static Object jsonFileToObject(String filePath) throws IOException {
        File file = new File(filePath);
        // 先读取为Object，然后根据实际类型处理
        return objectMapper.readValue(file, Object.class);
    }

    /**
     * 如果确定是对象(Map)
     */
    public static Map<String, Object> jsonFileToMap(String filePath) throws IOException {
        return objectMapper.readValue(new File(filePath), new TypeReference<Map<String, Object>>() {});
    }

    /**
     * 如果确定是数组(List)
     */
    public static List<Object> jsonFileToList(String filePath) throws IOException {
        return objectMapper.readValue(new File(filePath), new TypeReference<List<Object>>() {});
    }
    public static void main(String[] args) {
        try {
            List<Object> stringObjectList = jsonFileToList("D:\\develop\\projects\\python\\FastAPIProject\\utils\\cecily\\weibo_friends_3669102477.json");
            //System.out.println(stringObjectList);
            for (int i = 0; i < stringObjectList.size(); i++) {
                Map p = (Map) stringObjectList.get(0);
                System.out.println(p);
                System.out.println(p.get("id"));
                System.out.println(p.get("name"));
                System.out.println(p.get("screen_name"));
                System.out.println(p.get("location"));
                System.out.println(p.get("description"));
                System.out.println(p.get("followers_count"));
                break;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
